<template>
  <el-col>
    <el-form :model="form" label-width="120px">
      <el-form-item label="用户">
        {{ form.id }}
      </el-form-item>
      <el-form-item label="当前状态">
        {{ where }}
      </el-form-item>
      <el-form-item label="充电桩" v-if="where ==='充电区'">
        {{ station_id }}
      </el-form-item>
      <el-form-item label="修改充电量">
        <el-input v-model="form.amount" type="number"/>
        单位：度
      </el-form-item>
      <el-form-item label="修改充电模式">
        <el-radio-group v-model="form.mode">
          <el-radio label="T"/>
          <el-radio label="F"/>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">修改</el-button>
        <el-button type="primary" @click="onCancel">取消充电</el-button>
      </el-form-item>
    </el-form>
  </el-col>
</template>

<script>

import axios from 'axios';
import Global from './Global.vue';

export default {
  name: 'ChangeRequest',
  data() {
    return {
      form: {
        id: Global.id,
        amount: '',
        mode: '',
      },
      form1: {
        id: Global.id,
      },
      where: Global.where,
      station_id: Global.station_id,
    };
  },
  methods: {
    onSubmit() {
      const path = '/api/change_appoint';
      if (this.where !== '充电区') {
        if (this.form.amount !== '' && this.form.mode !== '') {
          axios.post(path, this.form)
            .then((res) => {
              if (res.data.ok) {
                Global.mode = this.form.mode;
                Global.amount = this.form.amount;
                this.$message.success('修改成功');
              } else {
                this.$message.success('修改失败');
              }
            })
            .catch((error) => {
              // eslint-disable-next-line
              console.error(error);
            });
        } else {
          this.$message.warning('充电量和充电模式不能为空');
        }
      } else {
        this.$message.warning('不能在充电区修改请求');
      }
    },
    onCancel() {
      const path = '/api/cancel_appoint';
      axios.post(path, this.form1)
        .then((res) => {
          if (res.data.ok) {
            this.$message.success('取消成功');
          } else {
            this.$message.success('取消失败');
          }
        });
    },
  },
};
</script>

<style scoped>

</style>
